using UnityEngine;
using UnityEngine.Rendering;

namespace UnityX
{
    /// <summary>
    /// 屏幕划线,没有宽度
    /// </summary>
    //[DisallowMultipleComponent]
    //[RequireComponent(typeof(Camera))]
    [AddComponentMenu("Effects/Screen Line")]
    public class ScreenLine : MonoBehaviour
    {
        //材质球
        public Material mat;
        public Color color = Color.white;

        private int m_PositionCount = 0;
        private Vector2[] m_Positions = new Vector2[0];

        public int positionCount
        {
            get { return m_PositionCount; }
            set
            {
                if (value > m_Positions.Length)
                {
                    Vector2[] p = new Vector2[value];
                    for (int i = 0; i < m_PositionCount; i++)
                    {
                        p[i] = m_Positions[i];
                    }
                    m_Positions = p;
                }
                m_PositionCount = value;
            }
        }
        public void SetPosition(int index, Vector2 position)
        {
            m_Positions[index] = position;
        }
        public void SetPositions(Vector2[] positions)
        {
            if (positions == null)
            {
                m_PositionCount = 0;
            }
            if (positions.Length > m_Positions.Length)
            {
                m_Positions = new Vector2[positions.Length];
            }
            for (int i = 0; i < positions.Length; i++)
            {
                m_Positions[i] = positions[i];
            }
            m_PositionCount = positions.Length;
        }
        public void AddPosition(Vector2 position)
        {
            if (m_PositionCount >= m_Positions.Length)
            {
                Vector2[] newPositions = new Vector2[m_Positions.Length + 100];
                for (int i = 0; i < m_Positions.Length; i++)
                {
                    newPositions[i] = m_Positions[i];
                }
                m_Positions = newPositions;
            }
            m_Positions[m_PositionCount] = position;
            m_PositionCount++;
        }
        public Vector2 GetPosition(int index)
        {
            return m_Positions[index];
        }
        public Vector2[] GetPositions()
        {
            if (m_Positions.Length == m_PositionCount)
            {
                return m_Positions;
            }
            Vector2[] positions = new Vector2[m_PositionCount];
            for (int i = 0; i < m_PositionCount; i++)
            {
                positions[i] = m_Positions[i];
            }
            return positions;
        }

        void OnEnable()
        {
            RenderPipelineManager.endCameraRendering += EndCameraRendering;
        }
        void OnDisable()
        {
            RenderPipelineManager.endCameraRendering -= EndCameraRendering;
        }
        void EndCameraRendering(ScriptableRenderContext context, Camera camera)
        {
            OnPostRender();
        }
        void OnPostRender()
        {
            if (m_PositionCount < 2)
            {
                return;
            }

            GL.PushMatrix();

            mat.SetColor("_Color", color);
            mat.SetColor("_BaseColor", color);
            mat.SetPass(0);

            GL.LoadOrtho();
            GL.Begin(GL.LINES);
            GL.Color(color);

            for (int i = 0; i < m_PositionCount; i++)
            {
                GL.Vertex3(m_Positions[i].x / Screen.width, m_Positions[i].y / Screen.height, 0);
            }

            GL.End();
            GL.PopMatrix();
        }
    }
}